<template>
  <div class="login">
    <h3 class="title">国民体质监测数据采集与质量控制平台</h3>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="off">

      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
          class="transparent-input"
        >
<!--          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"  />   auto-complete="off"-->
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"

          auto-complete="new-password"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
          class="transparent-input"
        >
<!--          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />-->
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder=""
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
<!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
<!--      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;margin-top: 4px"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span></span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    //this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          //alert(this.loginForm.password)
          this.$store.dispatch("Login", this.loginForm).then(() => {
            // 从sessionStorage获取状态，确保刷新后可用
            const hasUpperCase = sessionStorage.getItem('passwordHasUpperCase') === 'true';
           // alert(hasUpperCase)
            if(hasUpperCase){
              this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
            }else{
              this.$router.replace("/user/profile").catch(()=>{});
            }
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};


</script>

<style rel="stylesheet/scss" lang="scss" >
.login {
  display: flex;
 /* padding-left: 20%;
  padding-top: 12%;*/
  /*justify-content: center;*/
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.png");
  background-size: 100% 100%;
}
//.title {
//  width: 100%;
//  position: absolute;
//  text-align: center;
//  color: #fff;
//  letter-spacing: 6px;
//  font-weight: bold;
//  top: 10%;
//  font-size: 60px;
//}

.login-form {
  border-radius: 6px;
 /* background: rgba(255, 255, 255, 0.2);*/
  background-image: url("../assets/images/login-form.png");
  width: 319px;
  height: 290px;
  padding: 25px 25px 5px 25px;
  padding-top: 34px;
  margin-left: 20%;
  margin-top: 12%;
  .el-input {
    height: 38px;
    margin-bottom: 9px;
    input {
      height: 38px;
  /*    text-indent: 70px;*/
      text-align: center;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}

/* 美化 el-input 输入框 */
.el-input__inner {
  background-color: transparent;
  border:none;
  color: #f0f0f0;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
  font-size: 14px;
}

/* 输入框 placeholder 颜色改为 #fff */
::v-deep .el-input__inner::placeholder {
  color: #fff !important;
}

/* 输入框获得焦点时边框高亮 */
::v-deep .el-input .el-input__inner:focus {
  border-color: #409eff; /* element-ui 默认蓝色 */
  box-shadow: 0 0 5px rgba(64, 158, 255, 0.6);
  background-color: rgba(255, 255, 255, 0.15);
  color: #fff;
}

</style>
<style scoped lang="scss">
::v-deep .el-form-item__error{
  padding-top: 0!important;
}
::v-deep .el-button--primary {
  color: #FFFFFF;
  background-color: transparent;
  border-color:transparent;
  border-radius:0 ;
}
::v-deep .el-form-item--medium .el-form-item__content {
  background-color: transparent;
  border-color:transparent;

}
.title {
  width: 100%;
  position: absolute;
  text-align: center;
  color: #fff;
  letter-spacing: 6px;
  font-weight: bold;
  top: 8%;
  font-size: 64px;
}
/* 使用深度选择器覆盖Element UI默认样式 */
::v-deep .transparent-input .el-input__inner {
  background-color: transparent !important; /* 设置透明背景 */
  border: none; /* 保留边框，可选 */

}

/* 覆盖浏览器自动填充时的背景色 */
::v-deep .transparent-input .el-input__inner:-webkit-autofill,
::v-deep .transparent-input .el-input__inner:-webkit-autofill:hover,
::v-deep .transparent-input .el-input__inner:-webkit-autofill:focus,
::v-deep .transparent-input .el-input__inner:-webkit-autofill:active,
::v-deep .transparent-input .el-input__inner:-internal-autofill-selected {
  -webkit-box-shadow: 0 0 0 1000px transparent inset !important; /* 覆盖自动填充背景 */
  box-shadow: 0 0 0 1000px transparent inset !important; /* 标准语法 */
  -webkit-text-fill-color: #fff !important; /* 设置自动填充文字颜色 */
  transition: background-color 5000s ease-in-out 0s !important; /* 超长过渡效果 */

}
</style>
